package com.feiyuan.modules.sys.service.impl;

import com.feiyuan.modules.sys.entity.UserEntity;
import com.feiyuan.modules.sys.vo.UserRoleVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.modules.sys.mapper.UserRoleDao;
import com.feiyuan.modules.sys.entity.UserRoleEntity;
import com.feiyuan.modules.sys.service.UserRoleService;
import org.springframework.util.CollectionUtils;


@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRoleEntity> implements UserRoleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserRoleEntity> page = this.page(
                new Query<UserRoleEntity>().getPage(params),
                new QueryWrapper<UserRoleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveUserRole(Long userId, List<Long> roleIds) {
        List<UserRoleEntity> userRoleList = new ArrayList<>();
        for (Long roleId : roleIds) {
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            userRoleEntity.setUserId(userId);
            userRoleEntity.setRoleId(roleId);
            userRoleList.add(userRoleEntity);
        }
        this.saveBatch(userRoleList);
    }

    @Override
    public void selectIsRoles(UserEntity user) {
        List<UserRoleEntity> userRoleList = baseMapper.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id", user.getId()));
        if (CollectionUtils.isEmpty(userRoleList)) {
            // 直接保存角色与用户的对应信息
            if (!CollectionUtils.isEmpty(user.getRoleIds())) {
                this.saveUserRole(user.getId(), user.getRoleIds());
            }
        } else {
            List<Long> roleIds = new ArrayList<>();
            for (UserRoleEntity userRoleEntity : userRoleList) {
                roleIds.add(userRoleEntity.getRoleId());
            }

            if (!Arrays.equals(user.getRoleIds().toArray(new Long[0]), roleIds.toArray())) {
                baseMapper.delete(new QueryWrapper<UserRoleEntity>().eq("user_id", user.getId()));
                this.saveUserRole(user.getId(), user.getRoleIds());
            }
        }
    }

    @Override
    public void deleteUserRole(List<Long> userIds) {
        for (Long userId : userIds) {
            baseMapper.delete(new QueryWrapper<UserRoleEntity>().eq("user_id", userId));
        }
    }

    @Override
    public List<UserRoleVO> selectRoleList(String userId) {
        List<UserRoleVO> userRoleVOS = baseMapper.selectRoleList(userId);

        return userRoleVOS;
    }

    @Override
    public List<Long> selectByUserId(Long id) {
        List<UserRoleEntity> userRoles = baseMapper.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id", id));
        List<Long> roleIds = new ArrayList<>();
        for (UserRoleEntity userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        return roleIds;
    }

}